goog.require('goog.dom');
goog.require('goog.ui.Button');
//goog.require('goog.events');
goog.require('goog.ui.Control');

var ChatWidget = new Class.create({
	
	initialize: function() {
		
	},
	
	init: function(o) {
		this.userName = o['userName'];
//		this.title = o[DataConstants.TITLE];
//		this.headPanel  	= goog.dom.createDom('div', {'style': 'float:left;width:99%;height:20px;-moz-border-radius-topleft:5px;-moz-border-radius-topright:5px;background-color:#22476F;padding:2px;','id':DomConstants.HEADER_ICON_PANEL},this.title);
		
		
		this.txtMsgArea = goog.dom.createDom('textarea', {'rows':3, id:'txtAreaMsg','style':'width:80%;'},"");
//		this.test = goog.graphics.TextElement(element, graphics, stroke, fill); 
//		var msgPanel  = goog.dom.createDom('div', {'style': 'float:left;height:92%;border:1px solid red;width:80%','id':DomConstants.CHAT_MSG_DISPLAY_PANEL},this.txtMsgArea);
		
//		this.btnSendMsg  = new goog.ui.Button('send');
		this.txtMsg = goog.dom.createDom('input', {'type':'text','id':'txtmsg','class':'label-input-label'});
		
		var txtMsgSpan  = goog.dom.createDom('span', {'id':DomConstants.SPN_TXT_MSG},this.txtMsg);
		var btnSendMsgSpan  = goog.dom.createDom('span', {'id':DomConstants.SPN_BTN_SEND_MSG},'');
		
		
		this.userActionPanel  = goog.dom.createDom('div', {'style': 'float:right;height:100%;width:15%;border:1px solid red;background-color:white','id':DomConstants.CHAT_WRITE_MSG_PANEL},txtMsgSpan,btnSendMsgSpan);
//		this.bodyPanel  	= goog.dom.createDom('div', {'style': 'float:left;border:3px solid #22476F;width:97%;height:100%;padding:2px;','id':'aaaa'},msgPanel ,userActionPanel);
		this.chatPanel  = goog.dom.createDom('div', {'style': '','id':DomConstants.CHAT_MSG_PANEL},this.txtMsgArea);
		
//		 var txtMsg = new goog.ui.Container();
//		 txtMsg.decorate(goog.dom.$('tb4'));
//		 txtMsg.setKeyEventTarget(goog.dom.$('tb4_key_target'));
//		 txtMsg.setFocusable(true);

		
		//this.chatPanel  = goog.dom.createDom('div', {'style': 'float:left;height:600px;width:20%;border:1px solid;','id':DomConstants.CHAT_MSG_PANEL},msgPanel ,userActionPanel);
	},
	
	getContent: function() {
		return this.chatPanel;
	},
	
	draw: function () {
		this.chatCnt = new goog.ui.Control(this.getContent());
		this.chatCnt.addClassName("dashboard-content");
		this.chatCnt.addClassName("goog-inline-block");
		this.chatCnt.render(goog.dom.$('chatwidget'));
		
		this.userActionCnt = new goog.ui.Control(this.userActionPanel);
		this.userActionCnt.addClassName("dashboard-content");
		this.userActionCnt.addClassName("goog-inline-block");
		this.userActionCnt.render(goog.dom.$('meta'));
		
		this.chatWidget.getSendButton().render(goog.dom.$(DomConstants.SPN_BTN_SEND_MSG));
		
		goog.events.listen(chatWidget.getSendButton(),goog.ui.Component.EventType.ACTION,
			        function(e) {
//			 			alert('its me');	
			 			componentExecFunction('chat_instance','sendMessage',null);
			         });
	},
	
	getSendButton: function() {
		return this.btnSendMsg;
	},
	
	sendMessage: function() {
		//var txtMsg = goog.dom.getElement('txtMsg');
//		var txtMsgArea = goog.dom.getElement('txtAreaMsg'); 
//		txtMsgArea.value = txtMsgArea.value + "\n"+this.userName+":"+ txtMsg.value;
		
		//ChatServiceCenter.sendMessageToGameCenter(txtMsg.value);
		//txtMsg.value = "";
//		alert('Send Msg'+ txt);
		
	},
	
	showChatMessage: function(mesg) {
		var txtMsgArea = goog.dom.getElement('txtAreaMsg'); 
		txtMsgArea.innerHTML = txtMsgArea.innerHTML + "<br/>"+mesg.sender+":"+ mesg.msg;
		txtMsgArea.scrollTop=txtMsgArea.scrollHeight;
	},
	
	showAllChatMessage: function(mesg) {
		var txtMsgArea = goog.dom.getElement('txtAreaMsg'); 
		txtMsgArea.innerHTML ="";
		for(var i=0;i<mesg.length; i++){
			txtMsgArea.innerHTML = txtMsgArea.innerHTML + "<br/>"+mesg[i].sender+":"+ mesg[i].msg;
		}
		txtMsgArea.scrollTop=txtMsgArea.scrollHeight;
	}
	
	

});